#include "ivscontroller.h"

IVSController::IVSController(QObject *parent) :
    QObject(parent)
{
    ivsModel = new IVSModel();
    ivsMainWindow = new IVSMainWindow(ivsModel);
    ivsMainWindow->show();
    isRunning = false;

    connect(ivsMainWindow, SIGNAL(clickedPushButtunRun()), this, SLOT(run()));

    connect(ivsModel, SIGNAL(updateCurrentFunction()), ivsMainWindow, SLOT(updateTableFunction()));
    connect(ivsModel, SIGNAL(updateVideoSource()), ivsMainWindow, SLOT(updateVideoSource()));
    connect(ivsModel, SIGNAL(updateCurrentFunctionSettings()), ivsMainWindow, SLOT(updateTableFunction()));
    connect(ivsModel, SIGNAL(updateROI(bool)), ivsMainWindow, SLOT(updateROI(bool)));
    connect(ivsModel, SIGNAL(stateChanged(QString,QColor)), ivsMainWindow, SLOT(status(QString,QColor)));

    videoPlayer = new CVideoPlayer(ivsModel);
    intruderDetector = new CIntrudersDetector(ivsModel);
    restrictedAreaSupervisor = new CRestrictedAreaSupervisor(ivsModel);
    cameraStateMonitor = new CCameraStateMonitor(ivsModel);
    peopleTraker = new CPeopleTracker(ivsModel);

}

IVSController::~IVSController()
{
    stop();

    delete restrictedAreaSupervisor;
    delete intruderDetector;
    delete videoPlayer;

    delete ivsMainWindow;
    delete ivsModel;
}

void IVSController::init()
{
}

void IVSController::run()
{
    if(isRunning){
        stop();
        return;
    }
    isRunning = true;
    switch(ivsMainWindow->getCurrentFunction()){
    case IVS::Video_player:
        videoPlayer->run();
        break;
    case IVS::Intruders_detection:
        intruderDetector->run();
        break;
    case IVS::Restricted_area_survuillance:
        restrictedAreaSupervisor->run();
        break;
    case IVS::Fence_survuillance:

        break;
    case IVS::People_counting:

        break;
    case IVS::People_tracking:
        peopleTraker->run();
        break;
    case IVS::Theft_detection:

        break;
    case IVS::Object_left_behide_detection:

        break;
    case IVS::Camera_checks:
        cameraStateMonitor->run();
        break;
    }

}

void IVSController::stop()
{
    isRunning = false;
    switch(ivsMainWindow->getCurrentFunction()){
    case IVS::Video_player:
        videoPlayer->stop();
        break;
    case IVS::Intruders_detection:
        intruderDetector->stop();
        break;
    case IVS::Restricted_area_survuillance:
        restrictedAreaSupervisor->stop();
        break;
    case IVS::Fence_survuillance:

        break;
    case IVS::People_counting:

        break;
    case IVS::People_tracking:
        peopleTraker->stop();
        break;
    case IVS::Theft_detection:

        break;
    case IVS::Object_left_behide_detection:

        break;
    case IVS::Camera_checks:
        cameraStateMonitor->stop();
        break;
    }
}

void IVSController::ROI_add_point(int x, int y)
{
    ivsModel->ROI_add_point(x,y);
}

void IVSController::toogleMakingROI()
{
    ivsModel->toogleMakingROI();
}

const char* IVSController::SourceName()
{
    return ivsModel->CurrentVideoSource().SourceName();
}
